<% 
  if @resource.scope=='PRJ'

    conditions = "resource_id=:resource_id"
    values = {:resource_id => @resource.id}
    # in order to not display events linked to deleted snapshot, we build the SQL request with 'NOT IN' as most of the time, there won't be unprocessed snapshots
    snapshots_to_be_deleted = Snapshot.find(:all, :conditions => ["status='U' AND project_id=?", @resource.id])
    unless snapshots_to_be_deleted.empty?
      conditions << " AND snapshot_id NOT IN (:sids)"
      values[:sids] = snapshots_to_be_deleted.map { |s| s.id }
    end

    events=Event.find(:all, :conditions => [conditions, values], :order => 'event_date desc')
    
    categories = EventCategory.categories(true)
%>

<script type="text/javascript">
  function changeCategory(categoryId, widgetId) {
    var eventTableMaxSize = 8;
    $j('#events_' + widgetId + ' tr.event').hide();
    var elements;
    if (categoryId=='all') {
      elements = $j('#events_' + widgetId + ' tr.event');
    } else {
      elements = $j('#events_' + widgetId + ' tr.cat_' + categoryId);
    }
    var elementsToDisplay = elements.slice(0,eventTableMaxSize);
    if (elementsToDisplay.size()==0) {
      $j('#events_' + widgetId).hide();
      $j('#show_more_events_' + widgetId).hide();
      $j('#no_event_message_' + widgetId).show();
    } else {
      $j('#events_' + widgetId).show();
      $j('#no_event_message_' + widgetId).hide();
      elementsToDisplay.show();
      if (elements.size() > eventTableMaxSize) {
        $j('#show_more_events_' + widgetId).show();
      } else {
        $j('#show_more_events_' + widgetId).hide();
      }
    }
  }

  function showAllEvents(widgetId) {
    var selectedCategory = $j('#select_category_' + widgetId).val();
    if (selectedCategory=='all') {
      $j('#events_' + widgetId + ' tr.event').show();
    } else {
      $j('#events_' + widgetId + ' tr.cat_' + selectedCategory).show();
    }
    $j('#show_more_events_' + widgetId).hide();
  }
</script>


<h3><%= message('widget.events.title') -%> &nbsp;&nbsp;

  <select class="small" id="select_category_<%= widget.id -%>" onchange="changeCategory(this.value, <%= widget.id -%>);">
    <option value="all"><%= message('widget.events.all') -%></option>
    <% categories.each do |categ| %>
    <option value="<%= categ.name.parameterize -%>"><%= h message('event.category.' + categ.name, :default => categ.name) %></option>
    <% end %>
  </select>

  <script>
    $j(function() {
      $j('#select_category_<%= widget.id -%>').select2({ width: '150px' });
    });
  </script>

</h3>

<table id="events_<%= widget.id -%>" class="spaced data">
  <thead>
    <tr>
      <th colspan="4"></th>
    </tr>
  </thead>
  
  <tbody>
<%
   events.each do |event|
     categ = event.category
     profile_data={}
     if categ=='Profile' && event.event_data
       profile_data=Hash[*(event.event_data.split(';').map { |elt| elt.split('=') }.flatten)]
     end
%>
<tr class="<%= cycle 'even','odd' -%> event cat_<%= categ.parameterize -%>">
    <td x="<%= event.event_date -%>"><%= l(event.event_date.to_date) %></td>
    <td><%= h message('event.category.' + categ, :default => categ) %></td>
    <td>
      <%= link_to_if profile_data['key'] && profile_data['from'] && profile_data['to'], event.name,
                             :controller => 'profiles', :action => 'changelog', :key => profile_data['key'],
                             :since =>  profile_data['from'], :to => profile_data['to'] -%>
    </td>
    <td>
      <% unless event.description.blank? %>
        <i class="icon-info" title="<%= h event.description -%>"></i>
      <% end %>
    </td>
</tr>
<%
   end
%>
  </tbody>
</table>

<div id="no_event_message_<%= widget.id -%>" style="margin: 5px">
  <span class="empty_widget"><%= message('widget.events.no_event') -%></span>
</div>

<a href="#" onclick="showAllEvents(<%= widget.id -%>);" id="show_more_events_<%= widget.id -%>" class="action"><%= message('widget.events.show_more') -%></a>

<script type="text/javascript">
  changeCategory('all', <%= widget.id -%>);
</script>

<% end %>
